<template>
    <input :name="dtpname" class="form-control" v-model="time" :class="[myClass]" type="text" :id="id" :readonly="readonly">
</template>

<script>
    import '../vendor/datetimepicker/bootstrap-datetimepicker.js'
    import '../vendor/datetimepicker/bootstrap-datetimepicker.zh-CN.js'
    import '../vendor/datetimepicker/bootstrap-datetimepicker.css'

    export default {
        name: 'DateTimePicker',
        data() {
            return {
                id:'dt-'+Math.random().toString().split('.')[1],
                time: ''
            }
        },
        props:{
            value:{
                type:String
            },
            myClass:{
                type:String,
                default:''
            },
            dtpname:{
                type:String,
                default:''
            },
            readonly:{
                type:Boolean,
                default:true
            },
            startDate:{
                type:String,
                default:'2000-01-01 10:00'
            },
            endDate:String,
            startView:{
                type:String,
                default:'month'
            },
            minView:{
                type:String,
                default:'hour'
            },
            format:{
                type:String,
                default:'yyyy-mm-dd hh:ii'
            },
            minuteStep:{
                type:Number,
                default:1
            }
        },
        watch:{
            value(v,ov){
                this.time = this.value;
            },
            time(v,ov){
                if(v==''){
                    this.$emit('input','');
                }
            }
        },
        methods: {
            dateDefind() {
                var self = this;
                
                $('#'+self.id).datetimepicker({
                    language:'zh-CN',
                    minView: self.minView,
                    format: self.format,
                    todayBtn: 1,
                    autoclose: 1,
                    startView:self.startView,
                    minuteStep:self.minuteStep,
                    startDate:self.startDate,
                    endDate:self.endDate
                });
                $('#'+self.id).datetimepicker()
                    .on('changeDate', function (ev) {
                        var value = $('#'+self.id).val();
                        self.time = value;
                        self.$emit('input',self.time);
                    });
            }
        },
        mounted: function () {
            this.dateDefind();
            $('#'+self.id).datetimepicker('update');
        }
    }
</script>

<style scoped>

</style>